package com.xx.doublexchat.common.user.dao;

import com.xx.doublexchat.common.common.domain.enums.YesOrNoEnum;
import com.xx.doublexchat.common.user.domain.entity.User;
import com.xx.doublexchat.common.user.mapper.UserMapper;
import com.xx.doublexchat.common.user.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author xiaoXuan
 * @since 2023-12-30
 */
@Service
public class UserDao extends ServiceImpl<UserMapper, User> {

    /**
     * 通过OpenId来查询用户信息
     *
     * @param openId
     * @return
     */
    public User getByOpenId(String openId) {
        return lambdaQuery().eq(User::getOpenId, openId).one();
    }

    /**
     * 通过name来获取用户信息
     *
     * @param name
     * @return
     */
    public User getByName(String name) {
        return lambdaQuery().eq(User::getName, name).one();
    }

    /**
     * 更改名字
     *
     * @param uid
     * @param name
     */
    public boolean modifyName(Long uid, String name) {
        return lambdaUpdate()
                .eq(User::getId, uid)
                .set(User::getName, name).update();
    }

    /**
     * 佩戴徽章
     *
     * @param uid
     * @param itemId
     */
    public void wearingBadges(Long uid, Long itemId) {
        lambdaUpdate()
                .eq(User::getId, uid)
                .set(User::getItemId, itemId)
                .update();
    }

    /**
     * 更新用户状态
     *
     * @param id
     */
    public void invalidUid(Long id) {
        lambdaUpdate().eq(User::getId, id)
                .set(User::getStatus, YesOrNoEnum.YES.getStatus())
                .update();
    }

    /**
     * 通过uids获取用户信息
     * @param friendUids
     * @return
     */
    public List<User> getFriendList(List<Long> friendUids) {
        return lambdaQuery()
                .in(User::getId, friendUids)
                .select(User::getId, User::getActiveStatus, User::getName, User::getAvatar)
                .list();
    }
}
